package edu.bu.manateem.msg.heatmap;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

import edu.bu.manateem.msg.model.Compound;

public class HeatmapTarget
{
	private static final String SVG_NS = SVGDOMImplementation.SVG_NAMESPACE_URI;
	private static final String X = "x";
	private static final String Y = "y";
	
	private static final String HEIGHT = "height";
	private static final Integer DEFAULT_HEIGHT_VALUE = 2;
	private static final String DEFAULT_HEIGHT =
		HeatmapTarget.DEFAULT_HEIGHT_VALUE.toString();
	private static final String WIDTH = "width";
	private static final String RECT = "rect";
	private static final String STYLE = "style";
	private static final String TARGET_STYLE =
		"fill:green;fill-opacity:.3;stroke:black;stroke-width:0.3";
	private static final String ON_CLICK = "onclick";
	private static final String ID = "id";
	
	private Integer xCoordinate;
	private Integer yCoordinate;
	private Integer length;
	private Compound compound;
	private GeneralPath shape = new GeneralPath();
	
	public HeatmapTarget(int scanIndex,int mZ,int numberOfScans,Compound species)
	{
		xCoordinate = scanIndex;
		yCoordinate = mZ;
		length = numberOfScans;
		compound = species;
	}
	
	public void drawTarget(Graphics2D graphics,int imageHeight)
	{
		
		/*
		 * The length of the image refers to its length in the time dimension.
		 * The y-coordinate is the m/z value subtracted from image height, which
		 * also happens to correspond to the max m/z value.
		 */

		int adjustedYcoordinate = imageHeight - yCoordinate;
		
		// horizontal line
		graphics.drawLine(xCoordinate,adjustedYcoordinate,xCoordinate + length,
			adjustedYcoordinate);
		
		// vertical lines 3 pixels
		graphics.drawLine(xCoordinate,adjustedYcoordinate - 3,xCoordinate,
			adjustedYcoordinate + 3);
		graphics.drawLine(xCoordinate + length,adjustedYcoordinate - 3,
			xCoordinate + length,adjustedYcoordinate + 3);
		
		// draw label
		graphics.drawString(compound.getLabel(),xCoordinate,adjustedYcoordinate + 6);
	}
	
	/**
	 * @return the shape
	 */
	public Shape getShape(int imageHeight)
	{
		int adjustedYcoordinate = imageHeight - yCoordinate;
		shape.moveTo(xCoordinate,adjustedYcoordinate);
		
		// horizontal line
		shape.lineTo(xCoordinate + length,adjustedYcoordinate);
		
		// vertical lines
		shape.moveTo(xCoordinate,adjustedYcoordinate - 3);
		shape.lineTo(xCoordinate,adjustedYcoordinate + 3);
		
		shape.moveTo(xCoordinate + length,adjustedYcoordinate - 3);
		shape.lineTo(xCoordinate + length,adjustedYcoordinate + 3);
		
		return shape;
	}
	
	public Element getSvgElement(SVGDocument doc,int imageHeight)
	{
		Element element =
			doc.createElementNS(HeatmapTarget.SVG_NS,HeatmapTarget.RECT);
		
		Integer adjustedYcoordinate =
			imageHeight - yCoordinate - HeatmapTarget.DEFAULT_HEIGHT_VALUE;
		
		element.setAttributeNS(null,HeatmapTarget.X,xCoordinate.toString());
		element.setAttributeNS(null,HeatmapTarget.Y,
			adjustedYcoordinate.toString());
		
		element.setAttributeNS(null,HeatmapTarget.WIDTH,length.toString());
		element.setAttributeNS(null,HeatmapTarget.HEIGHT,
			HeatmapTarget.DEFAULT_HEIGHT);
		element.setAttributeNS(null,HeatmapTarget.ID,compound.getId());
		
		// label
		StringBuilder info = new StringBuilder();
		info.append(compound.getLabel());
		info.append("\\nstart scan = ");
		info.append(xCoordinate);
		info.append("\\nnumber of scans = ");
		info.append(length);
		element.setAttributeNS(null,HeatmapTarget.ON_CLICK,"alert(\""
			+ info.toString() + "\")");
		
		element.setAttributeNS(null,HeatmapTarget.STYLE,
			HeatmapTarget.TARGET_STYLE);
		
		return element;
	}
	
	/**
	 * @return the xCoordinate
	 */
	public int getxCoordinate()
	{
		return xCoordinate;
	}
	
	/**
	 * @return the yCoordinate
	 */
	public int getyCoordinate()
	{
		return yCoordinate;
	}
	
	/**
	 * @param xCoordinate the xCoordinate to set
	 */
	public void setxCoordinate(int xCoordinate)
	{
		this.xCoordinate = xCoordinate;
	}
	
	/**
	 * @param yCoordinate the yCoordinate to set
	 */
	public void setyCoordinate(int yCoordinate)
	{
		this.yCoordinate = yCoordinate;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("HeatmapTarget [label=");
		builder.append(compound.getLabel());
		builder.append(", length=");
		builder.append(length);
		builder.append(", xCoordinate=");
		builder.append(xCoordinate);
		builder.append(", yCoordinate=");
		builder.append(yCoordinate);
		builder.append("]");
		return builder.toString();
	}
	
}
